package com.xz.knowledgeDuplication.thread.state;

/**
 * 守护线程(随着用户线程结束而结束,不会一直执行)
 * @author 召
 * @date 2022-07-25 21:06:16
 */
public class Daemon {

    public static void main(String[] args) {
        Thread thread = new Thread(new God());
        // 将上帝设置为守护线程
        thread.setDaemon(true);
        // 启动守护线程
        thread.start();
        // 启动用户线程
        new Thread(new People()).start();
    }

}

class God implements Runnable {
    @Override
    public void run() {
        while (true) {
            System.out.println("上帝保佑着你");
        }
    }
}

class People implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("活着");
        }
        System.out.println("--- bye world ---");
    }
}
